package com.ruoyi.wetest.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.wetest.domain.WeTestGaspReq;
import com.ruoyi.wetest.domain.WeTestStatisticsInfoReq;
import com.ruoyi.wetest.domain.WeTestTransReq;
import com.ruoyi.wetest.domain.WetestStressStatistics;
import com.ruoyi.wetest.manager.WetestManager;
import com.ruoyi.wetest.service.IWetestStressStatisticsService;
import com.ruoyi.wetest.service.IWetestStressTransService;
import com.ruoyi.wetest.utils.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Allen
 * @description TODO
 * @Date 2024/4/26 20:06
 */
@RestController
@RequestMapping("/gapsapi")
public class WeTestGapsapiController extends BaseController {

    @Autowired
    private IWetestStressTransService wetestStressTransService;

    @Autowired
    private IWetestStressStatisticsService wetestStressStatisticsService;

    @Autowired
    private WetestManager wetestManager;

    @Autowired
    private ThreadPoolTaskExecutor executor;

    /**
     * 上报注册压力进程实例
     * url:http://101.91.19.182:9126/gapsapi/data_collect_v1/register_load?instanceid=0&nonce=0&secret_id=b0df571783f34ee693291e07dd91b1f4&t=1714447896&testid=68de5235f4ec43328d4ff56c7f869547&token=6d93085c7d84687aca14e9ad3630c946&sign=%2BjZv%2BWbwuRmCfen45RQnRrZAahj4eK90h1vJFoyUSEY%3D
     * resp:{"ret":0,"msg":"ok"}
     */
    @GetMapping("/data_collect_v1/register_load")
    public AjaxResult dataCollectV1RegisterLoad(WeTestGaspReq req) {
        int i = wetestManager.registerLoad(req);
        AjaxResult result;
        if (i != 0) {
            result = AjaxResult.error();
        } else {
            result = success();
        }
        return AjaxResult.toWetest(result);
    }

    /**
     * 上报事务数据
     */
    @PostMapping("/data_collect_v1/trans_info")
    public AjaxResult dataCollectV1TransInfo(WeTestGaspReq req, HttpServletRequest request) {
        String httpBody = HttpUtils.getHttpBody(request);
        WeTestTransReq weTestTransReq = JSON.parseObject(httpBody, WeTestTransReq.class);
        logger.debug("testId:{} trans size:{}",req.getTestid(), weTestTransReq.getData().size());
        AjaxResult result = success();
        wetestManager.transInfo(weTestTransReq, req.getTestid());
        executor.execute(new Runnable() {
            @Override
            public void run() {
                wetestStressTransService.saveBatch(weTestTransReq.getData(), req.getTestid(), req.getInstanceid());
            }
        });

        return AjaxResult.toWetest(result);
    }


    /**
     * 上报在线数据
     * url:http://101.91.19.182:9126/gapsapi/data_collect_v1/statistics_info?instanceid=0&nonce=0&secret_id=b0df571783f34ee693291e07dd91b1f4&t=1714447913&testid=68de5235f4ec43328d4ff56c7f869547&token=6d93085c7d84687aca14e9ad3630c946&sign=2NRv2ganaEZYfbNNR7NPFKeOutY5YGtP282CmjojVmk%3D
     * resp:{"ret":0,"msg":"ok"}
     */
    @PostMapping("/data_collect_v1/statistics_info")
    public AjaxResult dataCollectV1StatisticsInfo(WeTestGaspReq req, HttpServletRequest request) {
        logger.debug("si testId:{}", req.getTestid());
        String httpBody = HttpUtils.getHttpBody(request);
        WeTestStatisticsInfoReq weTestTransReq = JSON.parseObject(httpBody, WeTestStatisticsInfoReq.class);
        WetestStressStatistics statistics = new WetestStressStatistics();
        statistics.setTestId(req.getTestid());
        statistics.setcTime(System.currentTimeMillis());
        statistics.setInstanceId(req.getInstanceid());
        statistics.setRobotOnlineNum(weTestTransReq.getData().getRobot_online_num());
        statistics.setRobotTotalNum(weTestTransReq.getData().getRobot_total_num());
        statistics.setRecvPkgTotalNum(weTestTransReq.getData().getRecv_pkg_total_num());
        statistics.setSendPkgTotalNum(weTestTransReq.getData().getSend_pkg_total_num());
        wetestManager.statisticsInfo(statistics);
        executor.execute(new Runnable() {
            @Override
            public void run() {
                wetestStressStatisticsService.insertWetestStressStatistics(statistics);
            }
        });
        AjaxResult result = success();
        return AjaxResult.toWetest(result);
    }


}
